package com.wst;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {


        private int totalLines = 0;
        private int codeLines = 0;
        private int commentLines = 0;
        private int blankLines = 0;

        private List<File> javaFiles = new ArrayList<>();

        public static void main(String[] args) {

            String projectPath ="D:\\code\\spring-plugin-server-2\\src\\main\\java";
            Test counter = new Test();
            counter.analyzeProject(projectPath);
            counter.printResults();
        }

        public void analyzeProject(String projectPath) {
            File projectDir = new File(projectPath);
            if (!projectDir.exists() || !projectDir.isDirectory()) {
                System.err.println("Invalid project directory: " + projectPath);
                return;
            }

            // 收集所有Java文件
            collectJavaFiles(projectDir);

            // 统计每个文件
            for (File file : javaFiles) {
                analyzeFile(file);
            }
        }

        private void collectJavaFiles(File dir) {
            File[] files = dir.listFiles();
            if (files == null) return;

            for (File file : files) {
                if (file.isDirectory()) {
                    collectJavaFiles(file);
                } else if (file.getName().endsWith(".java")) {
                    javaFiles.add(file);
                }
            }
        }

        private void analyzeFile(File file) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line;
                boolean inCommentBlock = false;

                while ((line = reader.readLine()) != null) {
                    totalLines++;
                    line = line.trim();

                    if (line.isEmpty()) {
                        blankLines++;
                    } else if (inCommentBlock) {
                        commentLines++;
                        if (line.contains("*/")) {
                            inCommentBlock = false;
                        }
                    } else if (line.startsWith("/*")) {
                        commentLines++;
                        if (!line.endsWith("*/")) {
                            inCommentBlock = true;
                        }
                    } else if (line.startsWith("//")) {
                        commentLines++;
                    } else {
                        codeLines++;
                    }
                }
            } catch (IOException e) {
                System.err.println("Error reading file: " + file.getPath());
                e.printStackTrace();
            }
        }

        public void printResults() {
            System.out.println("========= 代码统计结果 =========");
            System.out.println("项目文件数: " + javaFiles.size());
            System.out.println("总行数: " + totalLines);
            System.out.println("代码行数: " + codeLines);
            System.out.println("注释行数: " + commentLines);
            System.out.println("空行数: " + blankLines);
            System.out.println("==============================");
        }

}
